// pages/FanInfo/PersonInfo/PersonInfo.js
// pages/FanInfo/PersonInfo/PersonInfo.js
const util = require('../../../utils/util');
const API = require('../../../common/constant');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    food: [],
    music: [],
    movies: [],
    book: [],
    place: [],
    sports: [],
    hasLabel: true,
    threeImages: []
  },
  toMoreEva: function () {
    wx.navigateTo({
      url: '../../evaluate/index?id=' + this.data.info.id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserInfo(options.id);
  },
  /**
   * 获取三张图片
   */
  getThreeImages: function(){
    // console.log(this.data.info);
    let allImages = this.data.info.imageDTOList;
    // console.log(allImages);
    let count = 0;
    let threeImages = [];
    for(let i = 0; i < allImages.length ;i++){
        if(count >= 3) break;
        threeImages.push(allImages[i].fileUrl);
        count++;
    }
    console.log(threeImages);
    this.setData({
      threeImages: threeImages
    })
  },
  /**
   * 获取用户基本信息
   */
  getUserInfo(id) {
    var that = this;
    util.http({
      url: API.URL.USER.FIND_USERINFO,
      data: {
        userId: id
      },
      success: res => {
        console.log(res.data);
        let newData = [];
        if((typeof(res.data.bussData.label) != 'undefined') && (typeof(res.data.bussData.hobby) != 'undefined')) {
          newData = this.ChangeInfoLabel(res.data.bussData);
          this.changeHobby(newData.hobby);
        } else if(typeof(res.data.bussData.hobby) != 'undefined') {
          this.changeHobby(res.data.bussData);
          this.setData({
            hasLabel: false
          })
        } else if(typeof(res.data.bussData.label) != 'undefined') {
          newData = this.ChangeInfoLabel(res.data.bussData);
        } else {
          this.setData({
            hasLabel: false
          })
        }
        let marks = []
        let array = res.data.bussData.pageData.bussData
        let comments = []        
        array.forEach((item, index) => {
          comments.push({
            avatar: array[index].attachmentUrl,
            name: array[index].nickname,
            sign: array[index].content,
            time: array[index].gmtCreated
          })
          marks.push(array[index].sign)
        })
        newData = this.changeActivityType(res.data.bussData);
        that.setData({
          info: newData,
          marks: marks,
          comments: comments
        });
        this.getThreeImages();
      },
      fail: rej => {
        console.log(rej);
      }
    })
  },
  /**
   * 修改hobby
   */
  changeHobby: function (data) {

    let currentHobbies = data.split('[')[1].split(']')[0].split(',')[0].split('{');
    let temp, t; // 临时变量
    let food = [], music = [], movies = [], book = [], place = [], sports = [];
    for (let i = 1; i < currentHobbies.length; i++) {
      temp = currentHobbies[i].split(':');
      t = temp[1].split('"')[1].split('，');
      switch (temp[0]) {
        case '"饮食"': food = t; break;
        case '"音乐"': music = t; break;
        case '"电影"': movies = t; break;
        case '"书"': book = t; break;
        case '"地方"': place = t; break;
        case '"运动"': sports = t; break;
      }
      t = [];
    }
    this.setData({
      food: food,
      music: music,
      movies: movies,
      book: book,
      place: place,
      sports: sports
    });
  },
  /**
   * 修改用户标签
   */
  ChangeInfoLabel: function (data) {
    let newdata = data;
    newdata.labels = [];
    if (data.label.length == 0) {
      return newdata;
    } else {
      let labels = data.label.split(';');
      for (let i = 0; i < labels.length; i++) {
        newdata.labels.push(
          {
            'text': labels[i]
          });
      }
      return newdata;
    }
  },
  /**
   * 修改活动类型
   */
  changeActivityType: function (data) {
    if (data.eventType == "chat") {
      data.activityType = "#解闷聊天#";
      data.activityColor = '#F4BC11';
    } else if (data.eventType == "partner") {
      data.activityType = "#美食搭伙#";
      data.activityColor = '#FF8023';
    } else if (data.eventType == 'find') {
      data.activityType = "#求邂逅#";
      data.activityColor = '#FE3232';
    }
    return data;
  },

  goToChat: function(){
    // console.log(this.data.info.id);
    let info = this.data.info;
    wx.navigateTo({
      url: `../../chat/chat?id=${info.id}&yourNickname=${info.nickname}&yourHeadImgUrl=${info.attachmentUrl}`,
    })
  },
  /**
   * 跳转到 他的相册
   */
  toAlbum: function (e) {
    wx.navigateTo({
      url: 'Album/Album?id='+ e.target.id
    });
  },
  /**
   * 添加关注
   */
  Following: function(){
    let that = this;
    if(this.data.info.attentionStatus == '已关注'){
      wx.showModal({
        title: '提示',
        content: '您确定要取消关注嘛？',
        confirmColor: '#FE3232',
        success: function(res) {
          if (res.confirm) {
            util.http({
              url: API.URL.USER.DELETE_ATTENTIONINFO,
              data: {
                id: that.data.info.id
              }, 
              success: res => {
                app.globalData.isNewFollowers = true;
                app.globalData.isNewFollowing = true;                
                app.showUserInfo('您已经取消关注该用户','none',1500);
                that.getUserInfo(that.data.info.id);
              },
              fail: err => {
                console.log(err);
              }
            })
          } else if (res.cancel) {
          }
        }
      })
    } else if(this.data.info.attentionStatus == '未关注'){
      wx.showModal({
        title: '提示',
        content: '您确定要关注该用户嘛？',
        confirmColor: '#FE3232',
        success: res => {
          if(res.confirm) {
            util.http({
              url: API.URL.USER.ADD_ATTENTIONINFO,
              data: {
                id: that.data.info.id
              },
              success: res => {
                app.showUserInfo('您已经成功关注该用户','none',1500);
                app.globalData.isNewFollowers = true;
                app.globalData.isNewFollowing = true;                   
                that.getUserInfo(that.data.info.id);
              },
              fail: err => {
                console.log(err);
              }
            })
          } else if(res.cancel) {
            // console.log('用户点击取消');
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // wx.switchTab({
    //   url: '../my/my'
    // });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

